/*
 *  libKara - Library for basic instant messaging functions
 *  Copyright (C) 2007 Markus Karnik
 *
 *  This program is free software; you can redistribute it and/or modify it 
 *  under the terms of the GNU General Public License as published by the
 *  Free Software Foundation; either version 3 of the License, 
 *  or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but
 *  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 *  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 
 *  for more details.
 *
 *  You should have received a copy of the GNU General Public License along 
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
 *  
 */

package de.karnik.libkara.proto.xmpp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;

import de.karnik.helper.LogFunctions;

/**
 * The XMPP buffered reader class implements the buffered reader an special log functions.
 * 
 * @author Markus Karnik - markus.karnik@my-design.org
 * @version 1.0
 * @since 1.0
 */
public class XmppBufferedReader extends BufferedReader {

	/**
	 * Constructs a XMPP buffered reader.
	 * 
	 * @param in The reader object to use.
	 */
	public XmppBufferedReader( Reader in ) {
		super( in, 8 );
	}
	
	/* (non-Javadoc)
	 * @see java.io.BufferedReader#read(char[], int, int)
	 */
	@Override
	public int read( char[] cbuf, int off, int len ) throws IOException {
		
		if( LogFunctions.DEBUG && LogFunctions.logLevel == LogFunctions.LOG_LEVEL_3 ) {
			StringBuffer sb = new StringBuffer();
			int j = 0;
			sb.append( "IN <~~~ " );
			
			for( int i = 0; i < len; i++ ) {
				if( cbuf[ i + off ] != 0 ) {
					sb.append( cbuf[ i + off ] );
					j++;
				}
				
				if( j > 0 && ( j % 100 ) == 0 ) {
					LogFunctions.log( sb.toString() );
					sb.delete( 0, sb.capacity() );
					sb.append( "        " );
				}
			}
			LogFunctions.log( sb.toString() );
		}
		
		return super.read( cbuf, off, len );
	}

}
